# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import concurrent.futures

from celery.utils.log import get_task_logger
from celery import shared_task, task

# logger = logging.getLogger('blog')
logger = get_task_logger(__name__)

from . import http


@task
def touch_new_erp():
    """
    找到刚加入的cron,获取其总页数,然后修改为准备状态
    """
    urls = ['http://121.41.18.160:10086/Login',
            'http://newerp.ngrok.xiaoya1618.cn:8080/Login']
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        future_to_url = {executor.submit(http.get, url): url for url in urls}
        for future in concurrent.futures.as_completed(future_to_url):
            # page = future_to_url[future]
            # data = future.result()
            r = future.result()
            logger.debug(r.status_code)


@task
def wuliu_api(url, method, model):
    r = None
    if method == 'GET':
        r = http.get(url,model)
    elif method == 'POST':
        r = http.post(url,model)
    else:
        raise ValueError('invalid method')
    r.raise_for_status()
    m = r.json()
    if m.get('code','000') == 200:
        return m
    else:
        raise AssertionError('状态码不正常')
